<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExpoter;
use App\Models\WalletLog;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class WalletlogController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('财务流水');
            $content->description('');
//            $content->breadcrumb(
//                ['text' => '首页', 'url' => '/admin'],
//                ['text' => '用户管理', 'url' => '/admin/users'],
//                ['text' => '编辑用户']
//            );
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑日志信息');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }



    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建日志信息');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(WalletLog::class, function (Grid $grid) {
            $excel = new ExcelExpoter();
            $excel->setAttr(
                ['id', '角色（0个人1商家2合伙人）','账号','类型（0收入1支付）','流动金额','变动后金额','标记','事由','支付方式（0余额，1微信，2支付宝）','创建时间','修改时间'],
                ['id', 'user.role','user.mobile','type','work_capital','now_money','sign','note','trace_way','created_at','updated_at'],'财务流水'
            );
            $grid->exporter($excel);

            $grid->disableCreateButton();
            // 去掉默认的id过滤器
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('user.mobile', '用户名');
                $filter->like('sign', '标记');
                $filter->like('note', '事由');
                $filter->equal('user.role','是否商家')->radio([
                    0    => '个人用户',
                    1    => '商家',
                    2    => '合伙人',
                ]);
                $filter->equal('type','类型')->radio([
                    0    => '支出',
                    1    => '收入',
                ]);
                $filter->in('trace_way','支付方式')->checkbox([
                    0    => '余额',
                    1    => '微信',
                    2    => '支付宝',
                ]);
                $filter->between('created_at', '时间筛选')->datetime();
            });
            $grid->actions(function ($actions){
                $actions->disableDelete();
            });

            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->column('user.role','角色')->display(function ($role){
                if ($role == '0'){
                    return "<span class='badge bg-yellow-gradient'>个人</span>";
                }elseif ($role == '1'){
                    return "<span class='badge bg-yellow-gradient'>商家</span>";
                }else if ($role == '2'){
                    return "<span class='badge bg-yellow-gradient'>合伙人</span>";
                }else{
                    return "<span class='badge bg-yellow-gradient'>无效角色</span>";
                }
            });
            $grid->column('user.mobile','用户名');
            $grid->column('type', '类型')->display(function ($way){
                if ($way == '0'){
                    return "<span class='badge bg-red-gradient'>支出</span>";
                }elseif ($way == '1'){
                    return "<span class='badge bg-aqua-gradient'>收入</span>";
                }
            });
            $grid->column('work_capital', '流动金额');
            $grid->column('now_money', '变动后金额');
            $grid->column('sign', '标记')->display(function ($sign){
                if(strpos($sign,'后台') !== false){
                    return "<span class='label label-info'>$sign</span>";
                }else{
                    return $sign;
                }
            });
            $grid->column('note', '事由')->display(function ($note){
                return isset($note) ? $note : '';
            });
            $grid->column('trace_way', '支付方式')->display(function ($way){
                if ($way == '0'){
                    return "<span class='badge bg-teal-gradient'>余额</span>";
                }elseif ($way == '1'){
                    return "<span class='badge bg-green-gradient'>微信</span>";
                }elseif ($way == '2'){
                    return "<span class='badge bg-blue-gradient'>支付宝</span>";
                }
            });
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(WalletLog::class, function (Form $form) {

            $form->display('id', '日志编号');
//            $form->display('user_id', '用户ID');
            $form->display('type', '类型');
            $form->display('work_capital', '流动金额');
            $form->display('now_money', '变动后金额');
            $form->display('sign', '标记');
            $form->display('trace_way', '支付方式');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
        });
    }

}
